from PyQt5 import QtCore, QtGui, QtWidgets
from tempfile import TemporaryFile
import ppt,traceback

class WorkProgress:
    signal = None

    resDict = {'percent': 0, 'result': True, 'message': ''}

    def __init__(self, signal):  # 从前端界面中传递参数到这个任务后台
        self.signal = signal

    def update(self,p):
        if p < 0:
            p = 0
        elif p > 100:
            p = 100

        print("current progress: %f" % p)
        self.resDict['percent'] = p
        self.resDict['result'] = True
        self.resDict['message'] = ''

        if self.signal:
            self.signal.emit(self.resDict)  # 任务完成后，发送信号

    def add(self,p):
        self.update(self.resDict['percent'] + p)

    def err(self,message):
        self.resDict['percent'] = 0
        self.resDict['result'] = False
        self.resDict['message'] = message
        self.signal.emit(self.resDict)  # 任务完成后，发送信号

class workthread(QtCore.QThread):  # 继承QThread
    resSignal = QtCore.pyqtSignal(dict)  # 注册一个信号

    def __init__(self ):  # 从前端界面中传递参数到这个任务后台
        super(workthread, self).__init__(None)

    def run(self):  # 重写run  比较耗时的后台任务可以在这里运行
        progress = WorkProgress(self.resSignal)
        try:
            p = ppt.ppt(progress)
            p.do()

        except Exception as err:
            exstr = traceback.format_exc()
            print(exstr)

            progress.err(str(err))

    #上传文件，返回url
    def upload_oss(self):
        pass

    #生成缩略图，返回图片列表
    def get_pngs(self,ppturl):
        pass

    #生成字幕
    def gen_srt(self):
        pass



